package top.suven.base.util;

public class IdUtil {
	
	public static IdUtil instance = null;
	private static final long ONE_STEP = 10;
	private static long lastTime = System.currentTimeMillis();
	private static short count = 0;

	/**
	* 获取id生成器类的单例对象
	* @return
	*/
	public static IdUtil getInstanse(){
	   if(instance == null){
	    instance = new IdUtil();
	   }
	   return instance;
	}

	
	private IdUtil(){
	}
	/**
	* 获取全局唯一的id ,id = 系统时间 +计数
	* @return
	*/
	public synchronized String getUID(){
		if (count == ONE_STEP) {
			 boolean done = false;
			 while (!done) {
				      long now = System.currentTimeMillis();
				      if (now == lastTime) {
					       try {
					        Thread.sleep(1);
					       } catch (java.lang.InterruptedException e) {
					       }
					       continue;
				      } else {
					       lastTime = now;
					       count = 0;
					       done = true;
				      }
			     }
		    }
	   String result = lastTime + "" + (count++);
	   return result;
	}

}
